package com.jack.lib.base

import android.os.Bundle
import android.view.View
import androidx.lifecycle.MutableLiveData
import androidx.viewbinding.ViewBinding
import com.chad.library.adapter.base.BaseQuickAdapter

/**
 * author : jack(黄冲)
 * e-mail : 907755845@qq.com
 * create : 2020/9/14
 * desc   :
 */


abstract class KtEntrustRefreshFragment<VB: ViewBinding, M> : KtRefreshFragment<VB>() {

    abstract val adapter: BaseQuickAdapter<M, *>

    protected val data by lazy { MutableLiveData<List<M>>() }

    override fun onRefresh() {
        load()
    }

    override fun onLoadMore() {
        load()
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        onRefresh()
        data.observe(viewLifecycleOwner) {
            if (page == DEFAULT_PAGE) {
                adapter.setList(it)
            }else{
                adapter.addData(it)
            }
            finishRefreshAndLoadMore(adapter)
        }
    }

    abstract fun load()
}
